package com.jky.trial.controller;

import com.jky.trial.annotation.NoRepeatSubmit;
import com.jky.trial.common.BaseResult;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.constant.PermissionConstant;
import com.jky.trial.entity.Project;
import com.jky.trial.entity.User;
import com.jky.trial.service.ProjectService;
import com.jky.trial.service.UserService;
import com.jky.trial.vo.ProjectVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/project")
public class ProjectController {

    private ProjectService recordService;
    private UserService userService;

    @Autowired
    ProjectController(UserService userService, ProjectService recordService) {

        this.userService = userService;
        this.recordService = recordService;
    }

    @PostMapping
    @NoRepeatSubmit
    public BaseResult create(@RequestBody Project record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setCreatorId(user.getId().toString());
        record.setDeleted(false);
        return recordService.create(record);
    }

    @PutMapping
    public BaseResult update(@RequestBody Project record) {
        return new BaseResult(true, recordService.update(record));
    }

    /**
     * 人员设置
     *
     * @param record
     * @return
     */
    @PutMapping("/setPerson")
    public BaseResult setPerson(@RequestBody Project record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        return new BaseResult(true, recordService.setPerson(record, user));
    }

    /**
     * 进度计划设置
     *
     * @param record
     * @return
     */
    @PostMapping("/setPlan")
    public BaseResult setPlan(@RequestBody Project record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        return new BaseResult(true, recordService.setPlan(record, user));
    }

    /**
     * record.id required
     * record.status 操作状态：1通过，0驳回
     * record.desc 审批意见
     * record.statusOutline 审批步骤
     *
     * @param record
     * @param request
     * @return
     */
    @PutMapping("/summaryCheck")
    public BaseResult setup(@RequestBody Project record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        return recordService.summaryCheck(record, user);
    }


    @RequiresPermissions(PermissionConstant.DELETE_PROJECT)
    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping("/detail/{id}")
    public BaseResult detail(@PathVariable Integer id) {
        return new BaseResult(true, recordService.moreDetail(id));
    }

    /**
     * 项目统计
     *
     * @return
     */
    @GetMapping("/statistic")
    public BaseResult statistic(ProjectVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        user = userService.detail(user.getId());
        return recordService.staticProject(record, user);
    }

    /**
     * 领导看板统计
     *
     * @param record
     * @param request
     * @return
     */
    @GetMapping("/staSummary")
    public ListResult summary(ProjectVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        user = userService.detail(user.getId());
        Subject subject = SecurityUtils.getSubject();//

        if (subject.isPermitted(PermissionConstant.COM_PROJECT)) {//被授予公司或部门项目权限
        } else if (subject.isPermitted(PermissionConstant.BUS_PROJECT)) {
            record.setBusId(user.getBusId());
        } else if (subject.isPermitted(PermissionConstant.DEP_PROJECT)) {//record.setBusId(user.getBusId());
            record.setDepId(user.getDepId());
        }
        return new ListResult(true, recordService.staSum(record));
    }

    @GetMapping
    public ListResult page(ProjectVo record) {
        return recordService.page(record);
    }


    @GetMapping("/withName")
    public ListResult pageWithName(ProjectVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        user = userService.detail(user.getId());
        Subject subject = SecurityUtils.getSubject();//
        if (
                subject.isPermitted(PermissionConstant.BUS_PROJECT) ||
                        subject.isPermitted(PermissionConstant.COM_PROJECT) ||
                        subject.isPermitted(PermissionConstant.DEP_PROJECT)) {
            if (subject.isPermitted(PermissionConstant.COM_PROJECT)) {//被授予公司或部门项目权限
                return recordService.pageWithName(record);
            } else if (subject.isPermitted(PermissionConstant.BUS_PROJECT)) {
                record.setBusId(user.getBusId());
            } else if (subject.isPermitted(PermissionConstant.DEP_PROJECT)) {//record.setBusId(user.getBusId());
                record.setDepId(user.getDepId());
            }
        } else {
            record.setOwnerId(user.getId());
        }
        return recordService.selfPageWithName(record);
    }

    @GetMapping("/outlineWithName")
    public ListResult outlineWithName(ProjectVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        user = userService.detail(user.getId());
        Subject subject = SecurityUtils.getSubject();//
        if (
                subject.isPermitted(PermissionConstant.BUS_PROJECT) ||
                        subject.isPermitted(PermissionConstant.COM_PROJECT) ||
                        subject.isPermitted(PermissionConstant.DEP_PROJECT)) {
            if (subject.isPermitted(PermissionConstant.COM_PROJECT)) {//被授予公司或部门项目权限
                return recordService.outlinePageWithName(record);
            } else if (subject.isPermitted(PermissionConstant.BUS_PROJECT)) {
                record.setBusId(user.getBusId());
            } else if (subject.isPermitted(PermissionConstant.DEP_PROJECT)) {//record.setBusId(user.getBusId());
                record.setDepId(user.getDepId());
            }
        } else {
            record.setOwnerId(user.getId());
        }
        return recordService.selfOutlinePageWithName(record);
    }

    @GetMapping("/list")
    public ListResult list(ProjectVo record) {
        return new ListResult(true, recordService.list(record));
    }
    @GetMapping("/descPage")
    public ListResult descPage(ProjectVo record) {
        return recordService.descPage(record);
    }

    /**
     * 个人项目情况
     *
     * @param record
     * @return
     */
    @GetMapping("/personSta")
    public BaseResult personSta(ProjectVo record) {
        return recordService.personSta(record);
    }

    /**
     * 项目明细
     *
     * @param record
     * @return
     */
    @GetMapping("/projectDetail")
    public BaseResult projectDetail(ProjectVo record) {
        return recordService.projectDetail(record);
    }

    /**
     * 部门统计
     * record.depId  部门ID
     * record.startDate    开始时间
     * record.endDate      截止时间
     *
     * @param record
     * @return
     */
    @GetMapping("/depSta")
    public BaseResult depSta(ProjectVo record) {
        return recordService.depSta(record);
    }

    /**
     * record.depId 某个部门参数
     * record.remark 不传递为新开开工项目统计
     *
     * @param record
     * @return
     */
    @GetMapping("/depStaMonth")
    public BaseResult depStaByMonth(ProjectVo record) {
        return recordService.depStaMonth(record);
    }

    /**
     * 业务板块统计
     *
     * @param record
     * @return
     */
    @GetMapping("/busSta")
    public BaseResult busSta(ProjectVo record) {
        return recordService.busSta(record);
    }

    @GetMapping("/busMonthSta")
    public BaseResult busMonthSta(ProjectVo record) {
        return recordService.busMonthSta(record);
    }

    @GetMapping("/comSta")
    public BaseResult comSta(ProjectVo record) {
        return recordService.comSta(record);
    }
    @GetMapping("/comMonthSta")
    public BaseResult comMonthSta(ProjectVo record) {
        return recordService.comMonthSta(record);
    }
}
